package settings;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.dwin.navy.serialportapi.com_track;
import com.dwin.navy.serialportapi.com_zhongji;
import com.imstlife.vending.R;
import com.imstlife.vending.activity.MyApplication;
import com.imstlife.vending.utils.Cof;
import com.imstlife.vending.utils.SharePersistentUtil;
import com.imstlife.vending.utils.T;
import com.imstlife.vending.vo.DrinkListVo;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CheckOutTradeAllFragment extends Fragment implements View.OnClickListener{
    private ScrollView sv;
    private LinearLayout ll;
    private int count;
    /**
     * 以勒串口API
     */
    private com_track yileSerial;

    /**
     * 中吉串口API
     */
    private com_zhongji zhongjiSerial;
    private String deviceModel;
    private boolean flag = false;
    private int num = 0;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootLayout = inflater.inflate(R.layout.fragment_checkouttradeall,
                container, false);
        rootLayout.findViewById(R.id.allcheck).setOnClickListener(this);
        rootLayout.findViewById(R.id.clear).setOnClickListener(this);
        sv = (ScrollView) rootLayout.findViewById(R.id.sv);
        ll = (LinearLayout) rootLayout.findViewById(R.id.ll);
        deviceModel = getActivity().getIntent().getStringExtra("deviceModel");
        count = ((DrinkListVo) getActivity().getIntent().getSerializableExtra("drinks")).getDrinkList().size();
        return rootLayout;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.clear:
                ll.removeAllViews();
                break;
            case R.id.allcheck:
                if (!flag){
                    flag = true;
                    out(1);
                }else{
                    setTv("正在检测");
                }
                break;
        }
    }

    public void out(final int dao){
        num++;
        new Thread() {
            @Override
            public void run() {
                String retstr = null;
                try {
                    int idao = dao;
                    if(Cof.MODEL_01.equals(deviceModel)){
                        retstr = yileSerial.chu_huo_track(idao);
                    }else if(Cof.MODEL_02.equals(deviceModel)||Cof.MODEL_03.equals(deviceModel)||Cof.MODEL_04.equals(deviceModel)){
                        retstr = zhongjiSerial.chu_huo_track(dao,deviceModel);
                    }

                } catch (Exception e) {
                    retstr = "Exception: " + e.getMessage();
                }

                final String hui = retstr;
                Log.d("out_return:","出货结果"+hui);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (null==hui||hui.length() == 0) {
                            setTv("货道"+dao+":出货成功");
                        } else {
                            setTv("货道"+dao+":"+hui);
                        }
                        if (num<count){
                            out(num+1);
                        }else{
                            flag = false;
                            setTv("检测结束--------------------------------------------------");
                        }
                    }
                });
            }
        }.start();
    }


    public void setTv(String s) {
        TextView tv = new TextView(getActivity());
        tv.setText(s);
        tv.setTextSize(25);
        tv.setPadding(20, 8, 0, 0);
        ll.addView(tv);
        sv.fullScroll(ScrollView.FOCUS_DOWN);
    }


    @Override
    public void onResume() {
        super.onResume();
        flag = false;
        num = 0;
        String str = SharePersistentUtil.getSharePersistentString(getActivity(),"spnum");
        int iserial = Cof.SERIAL_PORT_NUM;
        if (str!=null&&!str.equals("")){
            iserial = Integer.parseInt(str);
        }
        if(Cof.MODEL_01.equals(deviceModel)){
            yileSerial = new com_track(iserial);
            yileSerial.openSerialPort();
        }else if(Cof.MODEL_02.equals(deviceModel)||Cof.MODEL_03.equals(deviceModel)||Cof.MODEL_04.equals(deviceModel)){
            zhongjiSerial  = com_zhongji.getInstance(iserial);
            zhongjiSerial.openSerialPort();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if(Cof.MODEL_01.equals(deviceModel)){
            if(yileSerial!=null){
                yileSerial.closeSerialPort();
            }
        }else if(Cof.MODEL_02.equals(deviceModel)||Cof.MODEL_03.equals(deviceModel)||Cof.MODEL_04.equals(deviceModel)){
            if(zhongjiSerial!=null){
                zhongjiSerial.closeSerialPort();
            }
        }
    }
}
